home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15366 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  11.8 KB

  1. Path: news.uunet.ca!nobody
  2. Sender: nobody@banshee.uunet.ca
  3. X-Nntp-Posting-Host: nellie.cygnus.com
  4. Message-ID: <31647113.13BD8C66@cygnus.com>
  5. Date: Thu, 04 Apr 1996 20:02:11 -0500
  6. From: Kim Knuttila <krk@cygnus.com>
  7. X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.10 i586)
  8. MIME-Version: 1.0
  9. Newsgroups: comp.object,comp.lang.eiffel,comp.lang.c++,comp.lang.beta,comp.lang.java,comp.lang.sather
  10. CC: krk@cygnus.com
  11. Subject: Re: What Should An Exception Handling Do? -- Clarification of rules
  12. References: <Doq3sv.MzA@research.att.com> <1996Mar25.160011.13921@schbbs.mot.com>
  13. Content-Type: text/plain; charset=us-ascii
  14. Content-Transfer-Encoding: 7bit
  15.  
  16. [Aside: there's lots of newsgroups on the outbound line... I'll assume that all
  17.         are fascinated :-)] 
  18.  
  19. David L. Shang wrote:
  20. > In article <Doq3sv.MzA@research.att.com> bs@research.att.com (Bjarne Stroustrup
  21. > <9758-26353> 0112760) writes:
  22. > > billf@jovial.com (Bill Foote) writes
  23. > >
  24. > >
  25. > >  > > quote explaining why C++ (and Java) uses the termination model
  26. > >  > > of exception handling
  27. > >  >
  28. > >  > A cynic might conclude something more along these lines:  "It would be
  29. > >  > hard to implement resumable exceptions in C++, so they decided to punt."
  30. > >
  31. > > and that "cynic" would be wrong, guilty of not having done his homework,
  32. > > and guilty of making unkind conjectures without basis in facts.
  33. > >
  34. > But I don't see what's wrong with the cynic. It is true that to
  35. > implement resumable exceptions in C++ is very hard. It is also
  36. > true that resumable exception is useful, see below.
  37.  
  38. Hmm.. Well, it depends if you're interested in accuracy or not. If you
  39. want to believe that the committee did something for some reason, feel
  40. free to. For many things it may not even be totally incorrect. There
  41. are lots of people voting for lots of reasons. There were probably some
  42. votes that would agree with the cynic! (i.e. didn't want the 
  43. implementation grief.) But in this case, I don't believe the 
  44. majority did.
  45.  
  46. For what it's worth, I don't think the degree of difficulty of 
  47. implementation was a big issue. Most implementors present had 
  48. experience with building EH type systems in other languages. In 
  49. the debates (over a couple of years!), implementation strategies 
  50. were scoped out, and the relative merits were discussed (I loved 
  51. this part! :-). I believe most folk, at least the direct 
  52. participants in the extension work group, understood where
  53. resumption could be useful, as opposed to termination. After all, EH
  54. is not really a "new" thing, right? In the end, it boiled down
  55. to philosophy of language design and the fact that C++ is OO, not
  56. procedural. Perceived costs, perceived values, peoples experience, etc.
  57.  
  58. The entire discussion was very, very long, and very, very detailed.
  59. (I'm leaving out reams of interesting, germane stuff. My notes are
  60. packed and I can't exactly get at them. Actually, I'm probably
  61. optimistic in thinking I'll ever find them. If there's interest, I'll
  62. rummage after my move) 
  63.  
  64. And reasonable, intelligent people had differing opinions. Concensus 
  65. was arrived at eventually, but it took a lot of pretty thorough 
  66. discussion.
  67.  
  68. > > Here, I'd like to quote a key section:
  69. > >
  70. > >       Then, at the Palo Alto meeting in November 1991, we heard a
  71. > >       brilliant summary of the arguments for termination semantics
  72. > >       backed with both personal experience and data from Jim Mitchell
  73. > >       (from Sun, formerly from Xerox PARC). Jim had used exception
  74. > >       handling in half a dozen languages over a period of 20 years
  75. > >       and was an early proponent of resumption semantics as one of
  76. > >       the main designers and implementers of Xerox's Cedar/Mesa system.
  77. > >       His message was
  78. > >
  79. > >               ``termination is preferred over resumption; this is
  80. > >               not a matter of opinion but a matter of years of
  81. > >               experience. Resumption is seductive, but not valid.''
  82. > >
  83. > >       He backed this statement with experience from several operating
  84. > >       systems. The key example was Cedar/Mesa: It was written by people
  85. > >       who liked and used resumption, but after ten years of use, there
  86. > >       was only one use of resumption left in the half million line
  87. > >       system -- and that was a context inquiry. Because resumption
  88. > >       wasn't actually necessary for such a context inquiry, they removed
  89. > >       it and found a significant speed increase in that part of the
  90. > >       system. In each and every case where resumption had been used
  91. > >       it had -- over the ten years -- become a problem and a more
  92. > >       appropriate design had replaced it. Basically, every use of
  93. > >       resumption had represented a failure to keep separate levels
  94. > >       of abstraction disjoint
  95. > >
  96.  
  97. This summary understandably doesn't really do justice. Jim Mitchell was
  98. extremely interesting. I believe he spoke for an hour or so, and then fielded
  99. questions on the work he's done, opinions held, etc. The body of work, the
  100. size and scale of it... it was just very very interesting. 
  101.  
  102. > >       Mary Fontana presented similar data from the TI Explorer system
  103. > >       where resumption was found to be used for debugging only, Aron
  104. > >        Insinga presented evidence of the very limited and nonessential
  105. > >       use of resumption in DEC's VMS, and Kim Knuttilla related exactly
  106. > >       the same story as Jim Mitchell for two large and long-lived
  107. > >       projects inside IBM. To this we added a strong opinion in favor
  108. > >        of termination based on experience at L.M.Ericsson relayed to
  109. > >       us by Dag Bruck.
  110. > >
  111. > >       Thus, the C++ committee endorsed termination semantics.
  112. > >
  113. > The key point is: WHAT IS AN EXCEPTION?
  114.  
  115. Precisely. And the sister assumptions regarding frequency of use, (throw/catch
  116. infrequent relative to function calls, for example) and that kind of stuff
  117. to guide implementors on efficient implementations. 
  118.  
  119. And the other ones: when you are in a catch handler, is the stack unwound or
  120. not? (i.e. have the destructors run?) Who decides? When? What assumptions can
  121. be made at the throw point? Note that these are not unsolvable. In fact, quite
  122. the contrary. The termination model is almost a perfect subset of the resumption
  123. model. And the simplicity of it (not from the implementation point of view) did
  124. have some appeal. (i.e. some folks like that part in particular) The full model
  125. gets pretty elaborate, but it's definitely implementable in plausible time. (We
  126. implementors like job protection :-)
  127.  
  128. > The definition:
  129. > >       "Exception handling is intended to allow code that has encountered
  130. > >a condition it cannot cope with to return to some other code that
  131. > >directly or indirectly invoked it. There is no way for an exception
  132. > >handler to request the thread of control to resume from the throw point.
  133. > >In other words, "throw" implements the termination model of exception
  134. > >handling."     -ARM, Ellis & Stroustrup, page 354
  135. > >
  136. > narrows exceptions to error conditions only. If it is this case
  137. > termination would be sufficient, and resumptions would be useless.
  138.  
  139. And I think you've hit the nail on the head, so to speak. The committee
  140. endorsed the ARM model of exceptions. (i.e. errors) Of course, we all
  141. know that if you adopt a different paradigm (damn, I swore off that word)
  142. that you can view exception handling as "just another control flow mechanism."
  143. In fact, in the evenings (we don't get out much) some entertainment was 
  144. derived from "inventive use of resumption" coding examples. Goto's were 
  145. mundane in comparison. 
  146.  
  147. So I doubt that many would argue that there exist problems whose solution
  148. is elegantly expressed in the terminolgy of resumptive exception handling.
  149. (the favorite example in the "pro" camp was "the diskette drive is not ready")
  150.  
  151. There may be more arguments over the number of such problems, and whether
  152. the resumptive solution is the only elegant solution or just someone's 
  153. favorite paradigm.
  154.  
  155. But the major point is, as you point out, once you decide on the definition
  156. of "exception", much of the rest falls out. 
  157.  
  158. > But an exception is not necessarily an error. Sometimes it is an
  159. > condition that requires some extraordinary computation, a condition
  160. > that is not supposed for a regular case, for example, to open a
  161. > configuration file in an application directory and the file is not
  162. > found. This condition requires a further processing, e.g. to look up
  163. > the file in system directory. This is a quite common case in every
  164. > system design. More examples:
  165. >       * the font does not exist and a query to the user is required to
  166. >         get the substituting font;
  167. >       * the input is in the wrong type and an input retry is required;
  168. >       * the file is not associated with a default handler, would you
  169. >         like to associate one and let me try to re-open the file?
  170. >       * the format is not understood, would you suggest me a
  171. >         converter?
  172. >       * the embedded object is re-located, would give me the directory
  173. >         to the new place?
  174. > These conditions are not errors but only require some extraordinary work.
  175. > Similar examples were also given by Bill Foote in his previous post.
  176.  
  177. I didn't see Bill's post (It's been a while since I've had time to read. I
  178. probably still don't have time :-), but if someone kept it...?
  179.  
  180. > It is not hard for people to figure out more examples that requires the
  181. > following struture:
  182. >  result = try do_something()
  183. >           {
  184. >                 when condition1: some_extraordinary_work1; retry;
  185. >                 when condition2: some_extraordinary_work2; retry;
  186. >                 when condition3: some_extraordinary_work3; retry;
  187. >                 when condition4: return null;
  188. >           }
  189.  
  190. It's hard to make generalizations (I'll certainly be wrong on this
  191. one), but the design issue is deciding who has what responsibility,
  192. and if you want to abdicate responsibility, what's the best way to
  193. do so.
  194.  
  195. A great many can be solved by reviewing the object/responsibility 
  196. relationships that got us here. (Wow, that's a RealGeneralization(tm))
  197.  
  198. Somehow this feels like seeing OO code with lots of switch statements
  199. in it. It's not that "switch" is never used anymore. It's just that
  200. it's pretty infrequent in the stuff I've worked on in the past, so
  201. the occurrence was usually pretty special.
  202.  
  203. > is certainly better than:
  204. >         result = do_something();
  205. >         exceptional_condition = check_error_message();
  206. >         while (exceptional_condition)
  207. >         {
  208. >             if (exceptional_condition==condition4)
  209. >             {
  210. >                 result = null;
  211. >                 break;
  212. >             }
  213. >             switch (exceptional_condition)
  214. >             {
  215. >                 case condition1:
  216. >                         some_extraordinary_work1;
  217. >                         do_something();
  218. >                         break;
  219. >                 case condition2:
  220. >                         some_extraordinary_work2;
  221. >                         do_something();
  222. >                         break;
  223. >                 case condition3:
  224. >                         some_extraordinary_work3;
  225. >                         do_something();
  226. >                         break;
  227. >             }
  228. >         }
  229. > Oh, well! It takes me ten time longer to figure out the second piece of
  230. > code and I am still not sure whether this code is correct or not. After
  231. > a second look, yes, there are errors! "check_error_message()" should also
  232. > be called in the loop to get the new exceptional condition after a retry.
  233.  
  234. Yep. You could try recursion for some of these types of problems too. But it's
  235. still the case that if you really, really have a problem that can only be
  236. solved elegantly using resumption, then you'll be doing something like this. 
  237.  
  238. > David Shang
  239.  
  240. -- 
  241. Regards,
  242. krk.
  243.  
  244. Kim Knuttila             | Do I need to say it? well alright...
  245. Cygnus Support           | I don't Speak for Cygnus, They don't Speak for me.
  246. Toronto, Ontario         | I don't even Speak for Jake....
  247. bus: krk@cygnus.com      | But I do speak for myself...   Woof.
  248.